package com.jlastu.dlxy_androidapp.view.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.SchoolDetailsContract;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;
import com.jlastu.dlxy_androidapp.presenter.SchoolDetailsPresenter;
import com.jlastu.dlxy_androidapp.presenter.SchoolPresenter;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SchoolDetailsActivity extends AppCompatActivity implements View.OnClickListener, SchoolDetailsContract.ISchoolDetailsView {
    public static final String URL_DETAILS = "file:///android_asset/page/details.html";

    private Integer SchoolID;
    private WebView mWebView;

    private SchoolDetailsContract.ISchoolDetailsPresenter mSchoolPresenter;

    public String title;
    public String know;

    public String photo;
    public Bitmap bitmap;
    private List<School> mSchoolList;

    private List<SchoolPhoto> mPhotoList;

    private ImageButton mButton;
    private ImageButton mSelectButton;

    private TextView mTitle;

    @Override
    public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检查屏幕方向是否为横屏
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Orientation", "Landscape detected");

            // 创建并启动新的Activity
            Intent intent = new Intent(this, LandscapeActivity.class);
            startActivity(intent);

            // 可选：关闭当前Activity
            finish();
        }
    }
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_main);
        mSchoolPresenter = new SchoolDetailsPresenter(this, this);
        mSchoolList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        mWebView = findViewById(R.id.webview);
        mButton = findViewById(R.id.back_button);
        mSelectButton = findViewById(R.id.select);
        mTitle = findViewById(R.id.title_text);
        mButton.setOnClickListener(this);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new WebAppInterface(), "myInterface");
//        mWebView.loadDataWithBaseURL(null, photo, "text/html", "UTF-8", null);
        mWebView.loadUrl(URL_DETAILS);

        Intent intent = getIntent();
        SchoolID = intent.getIntExtra("school_id", 0);
        Log.d("TAG", "onCreate: " + SchoolID);

        mSchoolPresenter.getSchoolList(SchoolID);

        mSelectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateWebView();
            }
        });
    }



    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void setSchoolList(List<School> schoolList, List<SchoolPhoto> photoList) {
        mSchoolList = schoolList;
        mPhotoList = photoList;
        Log.d("", "showSchoolList " + mSchoolList.size());
        for (School school : mSchoolList) {
            Log.d("TAG", "School Name: " + school.getName());
            title = school.getName();
            know = school.getKnow();
        }
        for (SchoolPhoto schoolphoto : mPhotoList) {
//            photo = schoolphoto.getBitmap();
            byte[] imageData = schoolphoto.getData(); // 从数据库获取的图片数据
            bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
        }
        updateWebView();
        mTitle.setText(title);
    }

    public void updateWebView() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        photo = Base64.encodeToString(byteArray, Base64.DEFAULT);
        String js = "javascript:updateNews(" +
                "\"" + title + "\"," +
                "\"" + "1" + "\"," +
                "\"" + "1" + "\"," +
                "\"data:image/png;base64," + photo + "\"," +
                "\"" + know + "\")";
        mWebView.loadUrl(js);
    }

    public class WebAppInterface {


        @JavascriptInterface
        public void goBack(boolean showToast, String currentTitle) {
            // 此方法在html页面中被调用
            if (showToast) {
                Toast.makeText(SchoolDetailsActivity.this, "当前标题:" + currentTitle, Toast.LENGTH_SHORT).show();
            }
            finish();
        }
    }
}
